package com.letter.otools.day;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class BaseFragment extends Fragment {


    protected abstract int getLayout();

    protected abstract void initView(View view);

    protected void initData() {
    }

    protected void initBinder(View view) {
    }

    protected ActivityResultLauncher<Intent> resultLauncher = null;

    protected void onResultData(int resultCode, Intent intent) {
    }

    protected void onLunchResult(ActivityResult result) {
        onResultData(result.getResultCode(), result.getData());
    }

    protected void launchForResult(Intent intent) {
        if (resultLauncher != null) {
            resultLauncher.launch(intent);
        }
    }

    protected void launchForResult(Class<?> cls) {
        Intent intent = new Intent(getContext(), cls);
        if (resultLauncher != null) {
            resultLauncher.launch(intent);
        }
    }

    protected void registerForActivityResult() {
        resultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    onLunchResult(result);
                });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(this.getLayout(), container, false);
        initData();
        initBinder(view);
//        unBinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        if (unBinder != null) {
//            unBinder.unbind();
//        }
    }
}
